<?php
/*
 * stuQue控制器  学生得答题情况
 * Tobey
 * */
require_once "baseController.class.php";
class stuQueController extends baseController{
	/*
	获取学生做某张试卷的结果
	ajax  qus_id  stu_id
	*/
	public function getStuQuizResult(){
		if(IS_AJAX && IS_LOCAL && isset($_POST["qus_id"],$_POST["stu_id"])){
			$data = $this->quizPower();
			if($data["status"] == 1){
				$sql = D();		//开启数据库连接
				//获取学生的基本信息
				$d["student"] = $sql->table(array("student"=>""))->where(array("stu_id"=>$_POST["stu_id"]))->field(array("stu_id","stu_number","stu_name","stu_email","sex"))->find();
				// 获取学生的成绩情况
				$result = $sql->table(array("student_result"=>""))->where(array("qus_id"=>$_POST["qus_id"],"stu_id"=>$_POST["stu_id"]))->field(array("stu_score"))->find();
				if(!$result){
					$result = "未交";
				}
				$d["student"]["stu_score"] = $result;
				//获取试卷的基本信息
				$d["quiz"] = $sql->table(array("question_set"=>""))->where(array("qus_id"=>$_POST["qus_id"]))->field(array("qus_id","qus_name","qus_status","qus_remark","qus_create_time"))->find();
				//获取试卷的所有题目信息
				$d["question"] = $sql->table(array("question"=>""))->where(array("qus_id"=>$_POST["qus_id"]))->order("que_num")->select();
				if(!$d["question"]){
					$d["question"] = array();
				}
				$question_len = count($d["question"]);
				// 获取学生每一个题目的答题情况
				$field = array("stu_answer","is_correct");
				$table = array("student_question"=>"");
				for($i = 0;$i<$question_len;$i++){
					$stu_answer = $sql->table($table)->where(array("que_id"=>$d["question"][$i]["que_id"],"stu_id"=>$_POST["stu_id"]))->field($field)->find();
					if(!$stu_answer){
						$stu_answer["stu_answer"] = null;
						$stu_answer["is_correct"] = 0;
					}
					$d["question"][$i]["stu_answer"] = $stu_answer["stu_answer"];
					$d["question"][$i]["is_correct"] = $stu_answer["is_correct"];
				}
				$data = $this->message(1,"获取信息成功!",$d);
				unset($d,$sql,$question_len,$field,$table);
			}
			$this->ajaxReturn($data);
			unset($data);
		}
	}

	/*
	学生答题
	ajax  本地  que_id  stu_answer
	 */
	public function answer(){
		if(IS_AJAX && IS_LOCAL && isset($_POST["que_id"])){
			$data = $this->stuQuePower();
			if($data["status"] == 1){
				$sql = D();		//开启数据库
				session_start();
				$stu_id = $_SESSION["id"];
				session_write_close();
				$qus = $sql->table(array("question_set"=>"qs","question"=>"q"))->where(array("q.que_id"=>$_POST["que_id"],"__string__"=>"q.qus_id = qs.qus_id"))->field(array("qs.qus_status","qs.qus_id"))->find();
				if($qus && $qus["qus_status"] == 1){
					$que = $sql->table(array("question"=>""))->where(array("que_id"=>$_POST["que_id"]))->field(array("que_answer"))->find();
					if($que["que_answer"] == $_POST["stu_answer"]){
						$_POST["is_correct"] = 1;
					} else{
						$_POST["is_correct"] = 0;
					}
					unset($que);
					$_POST["stu_id"] = $stu_id;
					if($sql->table(array("student_question"=>""))->where(array("que_id"=>$_POST["que_id"],"stu_id"=>$_POST["stu_id"]))->count() == 0){
						if(is_numeric($sql->table(array("student_question"=>""))->add($_POST))){
							$data = $this->message(1,"答题成功!","");
						} else{
							$data = $this->message(0,"答题失败!","");		//联系开发者
						}
					} else{
						if($sql->table(array("student_question"=>""))->where(array("que_id"=>$_POST["que_id"],"stu_id"=>$_POST["stu_id"]))->update(array("stu_answer"=>$_POST["stu_answer"],"is_correct"=>$_POST["is_correct"]))){
							$data = $this->message(1,"答题成功!","");
						} else{
							$data = $this->message(1,"答题成功!","");
						}
					}
					unset($sql,$stu_id);
				} else{
					$data = $this->message(0,"试卷已经结束,无法修改题目答案!","");
				}
			}
			$this->ajaxReturn($data);
			unset($data);
		}
	}

	/**
  	 * 析构函数不用写任何内容也是可以的
  	 */
	public function __destruct(){}
}
